package com.sikao.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.TenderBidRecordAdapter;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.TenderBidBean;
import com.sikao.app.bean.TenderBidOperateBean;
import com.sikao.app.bean.TenderBidRecordSubmitBean;
import com.sikao.app.dao.TenderBidOperateModel;
import com.sikao.app.dao.TenderBidRecordModel;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.myinterface.CallbackOne;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.ListViewShow;
import com.sikao.app.view.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 买家中心我的单个招标对应的投标记录
 */
public class TenderBidRecordActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private RefreshListView refreshlistview;
	private TenderBidRecordAdapter adapter;
	private int type = 0;//0正常加载，1刷新，2加载更多
	private int page = 1;
	private int allPage;
	private TenderBidRecordModel tenderBidRecordModel;
	private TenderBidOperateModel tenderBidOperateModel;
	private TenderBidOperateBean tenderBidOperateBean;
	private TenderBidRecordSubmitBean bean;
	private List<TenderBidBean.ResBean.ListBean> beans;
	private int position;
	private AlertDialog dialog;
	private String beanId;
	private int operate;//1接受，2拒绝
	private View noData, noNetwork;//无数据和无网络布局
	private ListViewShow listViewShow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_tender_bid_record);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		beanId = getIntent().getStringExtra("bean");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		refreshlistview = (RefreshListView) findViewById(R.id.refreshlistview);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		refreshlistview.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				page = 1;
				type = 1;
				getList(false);
			}

			@Override
			public void onLoadMore() {
				page++;
				type = 2;
				if (page > allPage) {
					Toast.makeText(TenderBidRecordActivity.this, "暂无投标", Toast.LENGTH_SHORT).show();
					refreshlistview.onLoadMoreComplete();
				} else {
					getList(false);
				}
			}
		});
	}

	@Override
	protected void init() {
		titleTv.setText("投标记录");
		backIv.setVisibility(View.VISIBLE);
		beans = new ArrayList<>();
		adapter = new TenderBidRecordAdapter(this, beans, new CallbackOne() {
			@Override
			public void notice(String type, int position) {
				TenderBidRecordActivity.this.position = position;
				if (type.equals("yes")) {
					bidOperate(1);
				} else {
					bidOperate(2);
				}
			}
		});
		refreshlistview.setAdapter(adapter);
		bean = new TenderBidRecordSubmitBean();
		tenderBidRecordModel = new TenderBidRecordModel(this, new OnNetBackListener<TenderBidBean>() {
			@Override
			public void netBack(TenderBidBean data) {
				if (type == 1) {
					refreshlistview.onRefreshComplete();
				}
				if (type == 2) {
					refreshlistview.onLoadMoreComplete();
				}
				//无网络
				if (data == null) {
					createListViewShow();
					listViewShow.showNoNetwork();
					return;
				}

				if (data.getStatus().equals("1")) {
					allPage = data.getRes().getAllPage();
					if (type != 2 && beans.size() != 0) {
						beans.clear();
					}
					beans.addAll(data.getRes().getList());
					adapter.notifyDataSetChanged();
				} else {
					//无数据
					if (data.getStatus().equals("0")) {
						createListViewShow();
						listViewShow.showNoData("暂无投标记录");
					} else {
						Toast.makeText(TenderBidRecordActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		getList(true);
	}

	private void createListViewShow() {
		if (listViewShow == null) {
			listViewShow = new ListViewShow(TenderBidRecordActivity.this, beans, refreshlistview, new Callback() {
				@Override
				public void notice(int position) {
					type = 0;
					page = 1;
					getList(true);
				}
			});
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			default:
				break;
		}
	}

	private void getList(boolean isShow) {
		bean.setThisPage(page + "");
		bean.setPageNum(HttpApi.LIST_NUMBER);
		bean.setInviteId(beanId);
		tenderBidRecordModel.request(JSONUtil.toJSON(bean), isShow);
	}

	/**
	 * @param type 1接受，2拒绝
	 */
	private void bidOperate(int type) {
		operate = type;
		if (tenderBidOperateModel == null) {
			tenderBidOperateModel = new TenderBidOperateModel(this, new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null) {
						return;
					}
					if (data.getStatus().equals("1")) {
						if (operate == 1) {
							beans.get(position).setResult("1");
						} else {
							beans.get(position).setResult("2");
						}
						adapter.notifyDataSetChanged();
					} else {
						Toast.makeText(TenderBidRecordActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		tenderBidOperateBean = new TenderBidOperateBean();
		tenderBidOperateBean.setInviteId(beanId);
		tenderBidOperateBean.setBidId(beans.get(position).getId());
		if (type == 1) {
			tenderBidOperateBean.setReply("yes");
		} else {
			tenderBidOperateBean.setReply("no");
		}
		if (dialog == null) {
			AlertDialog.Builder builder = new AlertDialog.Builder(TenderBidRecordActivity.this);
			builder.setMessage("确定执行此操作？");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					tenderBidOperateModel.request(JSONUtil.toJSON(tenderBidOperateBean), true);
					dialog.dismiss();
				}
			});
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			dialog = builder.create();
		}
		dialog.show();

	}

}
